package cj.web.marriage.controllers;

import java.util.EnumSet;
import java.util.List;

import net.paoding.rose.web.annotation.Param;
import net.paoding.rose.web.annotation.Path;
import net.paoding.rose.web.annotation.rest.Get;
import net.paoding.rose.web.annotation.rest.Post;
import net.paoding.rose.web.var.Model;
import net.sf.json.JSONObject;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;

import cj.utils.sms.SendSMSUtil;
import cj.web.admin.constants.MemuEnum;
import cj.web.login.controllers.BaseController;
import cj.web.marriage.constants.MarriageInterestingEnum;
import cj.web.marriage.constants.MarriageRegStatusEnum;
import cj.web.marriage.constants.RegAddressEnum;
import cj.web.marriage.dao.MattiageTJDAO;
import cj.web.marriage.dao.RefAddressDAO;
import cj.web.marriage.domain.MarriageReg;
import cj.web.marriage.domain.MarriageTJ;
import cj.web.marriage.domain.RefAddress;

@Path("marriagetj")
public class MarriageTJController extends BaseController{
	
	public final static String accountSid="8aaf0708559f32dd0155a061b0e70089";
	public final static String accountToken="ca8d98f8170b4293be592df629a8ea07";
	public final static String appId="8aaf070855ed9ef10155fd08aee90737";
	
	private static Logger logger = Logger.getLogger(MarriageTJController.class.getName());

	@Autowired
	private MattiageTJDAO mattiageTJDAO;
	@Autowired
	private RefAddressDAO refAddressDao;
	
	
	@Get("show")
	public String show(Model model,MarriageReg marriageReg)
	{
		//MemuEnum
        model.add("MemuEnum",MemuEnum.marriagetj);
		model.add("StatusEnum",EnumSet.allOf(MarriageRegStatusEnum.class));
		model.add("AddressEnum",EnumSet.allOf(RegAddressEnum.class));
		
		List<RefAddress> arealis = refAddressDao.queryAll();
		model.add("arealis",arealis);
		
		return "/views/marriage/marriageTJList.vm";
	}
	
	@Post("getList")
	public String queryList(MarriageTJ marriageTJ) {
		
		JSONObject jo = new JSONObject();
		List<MarriageTJ> objs = mattiageTJDAO.query(marriageTJ);
		
		for(MarriageTJ obj:objs){
			String interesting = obj.getInteresting();
			if(interesting==null || "".equals(interesting)){
				obj.setInterestingDesc("");
			}else{
				String interestingDesc = "";
				int size = interesting.split(";").length;
				for(int i=0;i<size;i++){
					interestingDesc+=MarriageInterestingEnum.getName(
							Integer.parseInt(interesting.split(";")[i].toString()))+"｜";
					obj.setInterestingDesc(interestingDesc);
				}
			}
		}
		
		int size = mattiageTJDAO.querysize(marriageTJ);
		jo.put("data", objs);
		jo.put("draw", marriageTJ.getDraw());
		jo.put("recordsTotal", size);
		jo.put("recordsFiltered", size);
		
		return "@"+jo.toString();
	}
	
	/**
	 * 通知号外
	 * @param model
	 * @param id
	 * @return
	 */
	@Get("tzhw")
	public String tzhw(Model model,@Param("id") int id)
	{
		MarriageTJ obj = mattiageTJDAO.queryById(id);
		
		if(obj!=null)
		{
			String manPhone = obj.getManPhone();
			String womanPhone = obj.getWomanPhone();
			String tempid = "102729";
			SendSMSUtil.sendSMS(accountSid, accountToken, appId, manPhone+","+womanPhone, tempid, null);
		}
		
		return "r:show";
	}
	
	/**
	 * 通知视频
	 * @param model
	 * @param id
	 * @return
	 */
	@Get("tzsp")
	public String tzsp(Model model,@Param("id") int id)
	{
		MarriageTJ obj = mattiageTJDAO.queryById(id);
		
		if(obj!=null)
		{
			String manPhone = obj.getManPhone();
			String womanPhone = obj.getWomanPhone();
			String tempid = "102727";
			SendSMSUtil.sendSMS(accountSid, accountToken, appId, manPhone+","+womanPhone, tempid, null);
		}
		return "r:show";
	}
	
	/**
	 * 十进制转换为二进制
	 * @param N
	 * @return
	 */
	private static String DecimalToBinary(int decimalNumber){
        String s="";
        for(int n=decimalNumber;n>0;n/=2){
            s=(n%2)+s;
        }
        return s;   
    }
	
	/**
	 * 二进制转换为十进制
	 * @param decimalNumber
	 * @return
	 */
	private int BinaryToDecimal(String binaryNumber) {
		return Integer.parseInt(binaryNumber,2);
	}
}